package com.ruoyi.livedemo.controller;

import com.ruoyi.livedemo.common.ApiResponse;
import com.ruoyi.livedemo.context.CurrentUserHolder;
import com.ruoyi.livedemo.dto.AlbumMediaRequest;
import com.ruoyi.livedemo.dto.PagedResult;
import com.ruoyi.livedemo.entity.AlbumMedia;
import com.ruoyi.livedemo.service.AlbumService;
import javax.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/album")
public class AlbumController {

    private final AlbumService albumService;

    @GetMapping
    public ApiResponse<PagedResult<AlbumMedia>> list(@RequestParam(defaultValue = "0") int page,
                                                     @RequestParam(defaultValue = "12") int size) {
        Long userId = CurrentUserHolder.getUserId();
        return ApiResponse.ok(albumService.list(userId, page, size));
    }

    @PostMapping
    public ApiResponse<AlbumMedia> create(@Valid @RequestBody AlbumMediaRequest request) {
        Long userId = CurrentUserHolder.getUserId();
        return ApiResponse.ok("爱的相册已新增", albumService.create(userId, request));
    }

    @PutMapping("/{id}")
    public ApiResponse<AlbumMedia> update(@PathVariable Long id, @Valid @RequestBody AlbumMediaRequest request) {
        Long userId = CurrentUserHolder.getUserId();
        return ApiResponse.ok("爱的相册已更新", albumService.update(userId, id, request));
    }

    @DeleteMapping("/{id}")
    public ApiResponse<Void> delete(@PathVariable Long id) {
        Long userId = CurrentUserHolder.getUserId();
        albumService.delete(userId, id);
        return ApiResponse.ok("爱的相册已删除", null);
    }
}
